<template>
  <ych-modal
    :fullscreen="false"
    :title="title"
    :visible="visible"
    :destroyOnClose="true"
    @cancel="handleCancel">
    <table-list ref="tableList" :radio="radio" :group-id="groupId"
                        @selected="handleOk"></table-list>
    <template slot="footer">
      <a-button key="back" @click="handleCancel">{{ $t('m.common.button.close') }}</a-button>
      <a-button key="handleClear" @click="handleClear">{{ $t('m.common.button.clear') }}</a-button>
      <a-button key="submit" type="primary" @click="handleOk">{{ $t('m.common.button.ok') }}</a-button>
    </template>
  </ych-modal>
</template>

<script>

import TableList from './TableList'

export default {
  name: 'SelectTableDialog',
  components: {
    TableList,
  },
  data() {
    return {
      title: '选择表',
      visible: false,
      radio: false,
      groupId:''
    }
  },
  methods: {
    handleCancel() {
      this.visible = false
      this.radio = false
    },
    handleOk() {
      if (this.radio) {
        if (this.$refs.tableList.selectedRowKeys.length === 0) {
          this.messageUtil.warning('请在当前页选择数据')
          return
        }
        this.$emit('selectFinished', this.$refs.tableList.selectedRowKeys, this.$refs.tableList.selectionRows)
        this.handleCancel()
      } else {
        let selectedRowKeys = this.$refs.tableList.selectedRowKeys
        let selectionRows = this.$refs.tableList.selectionRows
        if (selectedRowKeys.length === 0) {
          this.messageUtil.warning('请选择数据')
          return
        }
        this.$emit('selectFinished', selectedRowKeys, selectionRows)
        this.handleCancel()
      }
    },
    handleClear() {
      this.$emit('selectFinished')
      this.handleCancel()
    }
  }
}
</script>
